/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thing;

import java.io.File;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

/**
 *
 * @author Tharen
 */
public class AppLauncher extends Application {
    AddWorld addStage;
    static OptionsMenu optnsMenu;
    AnchorPane anchor = new AnchorPane();
    //Buttons
    Button btnAdd = new Button("Add");
    Button btnLoad = new Button("Load");
    Button btnRemove = new Button("Remove");
    Button btnOptions = new Button("Options");
    Button btnEdit = new Button("Edit");
    //Txt fields 
    final TextArea descriptionTxa = new TextArea("Description.");
    private final static TextArea userView = new TextArea();
    //CheckBox
    CheckBox cbMods = new CheckBox("Mods Required");
    //Image
    Image pic = new Image("pictures"+File.separator+"Splashlogo.jpg");
    ImageView picture = new ImageView(pic);
    //Create containers
    HBox top = new HBox(10);
    VBox left = new VBox(10);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     * @param primaryStage Main stage. This holds all of the objects on the main
     * window.
     */
    @Override
    public void start(final Stage primaryStage) {
        //Base stuff
        addStage = new AddWorld(StageStyle.DECORATED);
        optnsMenu = new OptionsMenu(StageStyle.DECORATED);
        final ListView<Worlds> worldsList = new ListView<>(addStage.wm.getWorlds());
        primaryStage.setScene(new Scene(anchor, 800, 600));
        primaryStage.setTitle("MC Solar System");
        userView.setEditable(false);
        descriptionTxa.setEditable(false);
        cbMods.setDisable(true);
        cbMods.setOpacity(100);
        picture.setFitHeight(245);
        picture.setPreserveRatio(true);
        picture.setSmooth(true);
        primaryStage.setResizable(false);
        //Button actions
        btnAdd.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                addStage.show();
                //System.out.print(addStage.getObservList().size());
            }
        });
        btnRemove.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                if (worldsList.getSelectionModel().isSelected(worldsList.getSelectionModel().getSelectedIndex())) {
                    WorldManager.removeWorld(worldsList.getSelectionModel().getSelectedItem());
                }else AppLauncher.toUser("You must select a world to remove.");
                
            }
        });
        btnLoad.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                //WorldManager.test();
                WorldManager.extractWorld(worldsList.getSelectionModel().getSelectedItem().getWorldName());
            }
        });
        btnOptions.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                optnsMenu.show();
                //WorldManager.test();
            }
        });
        btnEdit.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                if (worldsList.getSelectionModel().isSelected(worldsList.getSelectionModel().getSelectedIndex())) {
                    Worlds worldTemp = worldsList.getSelectionModel().getSelectedItem();
                    addStage.name.setText(worldTemp.getWorldName());
                    addStage.type.setText(worldTemp.getType());
                    addStage.description.setText(worldTemp.getDescription());
                    addStage.modsRequired.setSelected(worldTemp.isModsRequired());
                    addStage.show();
                }else{
                    toUser("You must select a world to edit!");
                }
            }
        });
        //World List Selection event.
        worldsList.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent arg0) {
                if (!worldsList.getSelectionModel().isEmpty()) {
                    Worlds worldTemp = worldsList.getSelectionModel().getSelectedItem();
                    descriptionTxa.setText(worldTemp.getDescription());
                    cbMods.setSelected(worldTemp.isModsRequired());
                    Image temp = new Image(WorldManager.getRoot()+ File.separator + worldTemp.getWorldName()+File.separator+worldTemp.getImage());
                    picture.setImage(temp);
                }
            }
        });
        //Set anchor side for containers.
        AnchorPane.setLeftAnchor(left, 10.0);
        AnchorPane.setTopAnchor(top, 10.0);
        //Add to main container.
        anchor.getChildren().add(top);
        anchor.getChildren().add(left);
        anchor.getChildren().add(descriptionTxa);
        anchor.getChildren().add(userView);
        anchor.getChildren().add(cbMods);
        anchor.getChildren().add(picture);
        //Add buttons to sub-containers.
        top.getChildren().add(btnAdd);
        top.getChildren().add(btnLoad);
        top.getChildren().add(btnRemove);
        top.getChildren().add(btnOptions);
        top.getChildren().add(btnEdit);
        left.getChildren().add(worldsList);
        //Relocate items
        descriptionTxa.relocate(338, 272);
        top.relocate(10, 0);
        left.relocate(0, 35);
        userView.relocate(10, 436);
        cbMods.relocate(338, 257);
        picture.relocate(338, 10);
        //Do on close
        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {

            @Override
            public void handle(WindowEvent arg0) {
                WorldManager.saveWorldValues();
            }
        });
        //Show all the things!
        primaryStage.show();
    }

    public static void toUser(String message) {
        userView.appendText(message);
        userView.appendText(System.lineSeparator());
    }
}
